<?php

require_once dirname(__FILE__) . '/../common/IValidatable.php';
require_once dirname(__FILE__) . '/../common/Validate.php';

class User implements IValidatable {
	public $id;
	public $username;
	public $firstName;
	public $lastName;
	public $email;
	public $role;
	public $house;	
	public $password;

	public function isValid() {
		return ($this->getErrorFlags() == 0);
	}

	public function getErrorFlags() {
		$usernameVR = Validator::username($this->username);
		$firstNameVR = Validator::name($this->firstName);
		$lastNameVR = Validator::name($this->lastName);
		$emailVR = Validator::email($this->email);
		$roleVR = Validator::userRole($this->role);
		$houseVR = Validator::house($this->house);
		$passwordVR = Validator::password($this->password);

		$this->username = $usernameVR->getValue();
		$this->username = $firstNameVR->getValue();
		$this->username = $lastNameVR->getValue();
		$this->username = $emailVR->getValue();
		$this->username = $roleVR->getValue();
		$this->username = $houseVR->getValue();
		$this->username = $passwordVR->getValue();

		$errorFlags = 0;
		$errorFlags = $errorFlags | $usernameVR->getErrorFlag();
		$errorFlags = $errorFlags | $firstNameVR->getErrorFlag();
		$errorFlags = $errorFlags | $lastNameVR->getErrorFlag();
		$errorFlags = $errorFlags | $emailVR->getErrorFlag();
		$errorFlags = $errorFlags | $roleVR->getErrorFlag();
		$errorFlags = $errorFlags | $houseVR->getErrorFlag();
		$errorFlags = $errorFlags | $passwordVR->getErrorFlag();

		return $errorFlags;
	}

	public function getFullName() {
		return $this->firstName . ' ' . $this->lastName;
	}	
	
	static public function create($username, $firstName, $lastName, $email = null, $role = null , $house = null, $password = null) {
		$self = new User();
		$self->username = $username;
		$self->firstName = $firstName;
		$self->lastName = $lastName;
		$self->email = $email;
		$self->role = $role;
		$self->house = $house;		
		$self->password = $password;
		return $self;
	}

	static public function createForUpdate($username, $email, $house, $role  = null) {
		$self = new User();
		$self->username = $username;
		$self->email = $email;		
		$self->house = $house;
		$self->role = $role;
		$self->firstName = null;
		$self->lastName = null;			
		$self->password = null;
		return $self;
	}
}
